package com.qxsx.midi.activity.exam;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.adapter.ChooseTimerAdapter;
import com.qxsx.midi.adapter.TimeChooseWheelAdapter;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.config.AppManager;
import com.qxsx.midi.model.Time;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SGStrife;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.wheel.WheelView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Zrj on 2015/9/10.
 */
public class ExamChooseTimeActivity extends BaseActivity {
    private CustomProgress customProgress;

    private ListView lv_timer;
    private ChooseTimerAdapter adapter;
    private List<Time> list;
    private TextView tv_year;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_exam_choosetime);
        setTitle("报名");
        setNavBtn(R.mipmap.back, 0);
        findById();


    }

    @Override
    protected void onResume() {
        super.onResume();
        inItData();
        lv_timer.setSelection(0);

        lv_timer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s = new String(list.get(position).getExam_time());
                String a[] = s.split(":");

                SPUtils.put(AppContext.getContext(), "star_hour", a[0]);
                SPUtils.put(AppContext.getContext(), "star_minute", a[1]);
                String b = new String(list.get(position).getExam_end_time());
                String c[] = b.split(":");

                SPUtils.put(AppContext.getContext(), "end_hour", c[0]);
                SPUtils.put(AppContext.getContext(), "end_minute", c[1]);

                SPUtils.put(AppContext.getContext(), "exam_id", list.get(position).getExam_id());
                adapter.setChoose(position);
            }
        });
        Button btn = (Button) findViewById(R.id.pwd_status);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (list.size() == 0) {
                    ShowToast("该考点今日已无考试时间");
                } else {
                    AppManager.getAppManager().addActivity(ExamChooseTimeActivity.this);
                    startActivity(new Intent(AppContext.getContext(), ExamEnrollActivtiy.class));
                }

            }
        });

    }

    private void choosetime() {
        customProgress = CustomProgress.show(ExamChooseTimeActivity.this, "请稍后....", true, null);

        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();

        String exam_time = (String) SPUtils.get(AppContext.getContext(), "exam_time", "");
        String exam_site_id = (String) SPUtils.get(AppContext.getContext(), "exam_site_id", "");

        try {
            object.put("exam_date", exam_time);
            object.put("exam_site_id", exam_site_id);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("507", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                Log.w("code", code + "");
                Log.w("code", message + "");
                Log.w("code", array + "");

                try {
                    list = new ArrayList<Time>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object1 = array.getJSONObject(i);
                        Time time = new Time();
                        String exam_time = object1.getString("exam_time");
                        time.setExam_time(object1.getString("exam_time"));
                        time.setExam_id(object1.getString("id"));
                        String time1 = exam_time;

                        SimpleDateFormat df = new SimpleDateFormat("HH:mm");

                        Date date = null;
                        try {
                            date = df.parse(time1);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        Log.w("DATA", date + "");
                        Date afterDate = new Date(date.getTime() + 1200000);
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                        String dateOk = simpleDateFormat.format(afterDate);
//                        Log.w("afterDate", afterDate + "");
//                        String s = new String(afterDate + "");
//                        String a[] = s.split("Wed Dec 31 ");
//                        Log.w("a[1]", a[1] + "");
//                        String b = new String(a[1]);
//                        String c[] = b.split(":00");
//                        Log.w("c[0]", c[0] + "");
                        time.setExam_end_time(dateOk);

                        list.add(time);
                    }
                    adapter.addData(list);
                    if (array.length() != 0) {
                        String s = new String(list.get(0).getExam_time());
                        String a[] = s.split(":");

                        SPUtils.put(AppContext.getContext(), "star_hour", a[0]);
                        SPUtils.put(AppContext.getContext(), "star_minute", a[1]);
                        String b = new String(list.get(0).getExam_end_time());
                        String c[] = b.split(":");

                        SPUtils.put(AppContext.getContext(), "end_hour", c[0]);
                        SPUtils.put(AppContext.getContext(), "end_minute", c[1]);


                        SPUtils.put(AppContext.getContext(), "exam_id", list.get(0).getExam_id());
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                customProgress.dismiss();

            }
        });

    }

    private void inItData() {
        list = new ArrayList<>();

        choosetime();

        adapter = new ChooseTimerAdapter(ExamChooseTimeActivity.this, list);
        lv_timer.setAdapter(adapter);


    }

    private void findById() {
        lv_timer = (ListView) findViewById(R.id.lv_timer);
        tv_year = (TextView) findViewById(R.id.tv_year);
//      passw_3 = (WheelView) findViewById(R.id.passw_3);
//      passw_4 = (WheelView) findViewById(R.id.passw_4);

    }


    @Override
    protected void HandleTitleBarEvent(BaseActivity.TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;

        }
    }
}
